define(function(require, exports, module) {
    'use strict';

    var rk = require('rk');



    $.widget('rk.updateApprovalDialogCtrl', $.rk.oaDialogCtrl, {
        _init: function() {
            var me = this;
            var elem = me.element;
            var opt = me.options;
            me._initApplyUpdate();
            me.getBtn('updateApproval').on('click', $.proxy(me.okClicked, me));
            me.getBtn('cancelUpdate').on('click', $.proxy(me.cancelClicked, me));
        },
        _initApplyUpdate: function() {
            var me = this;
            var elem = me.element;
            var opt = me.options;
            var url = '/json/oa_approval/to-update-apply.action';
            rk.post(url, {
                applyId: opt.applyId
            }).done(function(json) {
                if (json.status == 0) {
                    me.$currentUserId = json.data.currentUserId;
                    if (json.data.approvers.length > 0) {
                        json.data.hasApprovers = 1;
                    } else {
                        json.data.hasApprovers = 0;
                    }
                    $.each(json.data.approvers, function(i, o) {
                        if (o.icon == '') {
                            o.icon = DEFAULTS.USER_DEFAULT_ICON;
                        }
                    });
                    me._renderApplyUpdateContent(json.data);
                    me._bindApplyUpdateHandler(json.data);
                } else {
                    rk.closePopup(); //关闭dialog
                    $.msg(rk.i18n('LOAD_ERROR'));
                }
            });
        },
        _renderApplyUpdateContent: function(data) {
            var me = this;
            var elem = me.element;
            var html = rk.templateText(require('oa/tmpl/approval/tpl_approval_dialog_update.tpl'), data);
            elem.html(html);
        },
        _bindApplyUpdateHandler: function(data) {
            var me = this;
            var elem = me.element;
            var opt = me.options;
            elem.on('click', '#approverSelectA', function() {
                me._handleApproverSelectClick($(this));
            });
            elem.on('click', '#aplSelectConfirmA', function() {
                $('div.js_nc_show_approval_user').hide();
            });
            $('#selectCopyInput').multipeopleselector({
                position: {
                    my: 'left top+2'
                }
            });
            var copyV = [];
            $.each(data.copys, function(i, o) {
                copyV.push({
                    label: o.label,
                    value: o.value,
                    pic: o.pic
                });
            });
            $('#selectCopyInput').multipeopleselector("val", copyV);
            var applyTitle = $('<li class="rk-formrow" ><label>' + rk.i18n("APPROVAL_THEME") + '</label><input type="text" _id="title" name="title" value="' + data.currentApply.title + '" placeholder="' + data.flowName + '" maxlength="100"></li>')
            if (data.items) {
                $('#applyItemListDiv').rkAutoForm({
                    items: data.items,
                    data: data.itemData
                });
            } else {
                $("#applyItemListDiv").append('<ul class="pop_list crate-entity-list crate-approval-list"></ul>')
            }
            $('#applyItemListDiv ul').prepend(applyTitle);
            rk.repositionDialog();
            if (data.uploadFlg == 1) {
                me._createFileUpload(data);
            }
            if (data.businessFlg == 1) {
                me._createRelBusiness(data.relBusiness);
            }
        },
        _createFileUpload: function(data) {
            this.element.find('.fileinput-box').multifileuploader({
                url: '/json/global_uploadify/apply-upload.action',
                queueOptions: {
                    loadedTemplate: '<div class="uploadqueue-item"><a href="javascript:;" class="uploadqueue-item-close"><span>close</span></a><a href="/approval/file-download.action?fileName={{name}}&fileUrl={{fileUrl}}"><span class="uploadqueue-item-filename" title="{{name}}">{{name}}</span></a><div class="uploadqueue-item-progress"></div></div>',
                    resultParser: function(json) {
                        return json;
                    }
                },
                limitFileCount: 9,
                uploadedFiles: $.map(data.files, function(file) {
                    return {
                        name: file.name,
                        fileUrl: file.url,
                        size: file.size
                    }
                })
            });
        },
        _createRelBusiness: function(data) {
            var me = this;
            var elem = me.element;
            var opt = me.options;
            if ($('#account').size()) {
                me._accountAutoComplete();
            }
            if ($('#opp').size()) {
                me._oppAutoComplete();
            }
            if ($('#contact').size()) {
                me._contactAutoComplete();
            }
            if ($('#lead').size()) {
                me._leadAutoComplete();
            }
            if ($('#campaign').size()) {
                me._campaignAutoComplete();
            }
            if ($('#expenseAccount').size()) {
                me._expenseAccountAutoComplete();
            }
            if ($('#agent').size()) {
                me._agentAutoComplete();
            }
            if ($('#terminal').size()) {
                me._terminalAutoComplete();
            }
            if ($('#partner').size()) {
                me._partnerAutoComplete();
            }
            if ($('#contract').size()) {
                me._contractAutoComplete();
            }
            me._typeChange();
            $("#bussiness_type").trigger('selectmenuchange');
            $("#btnSelectBusiness").click(function() {
                var $link = $(this);
                if (data != null) {
                    $("#businessDiv").show();
                    $link.parent().remove();
                } else {
                    $link.parent().prepend('<span class="task_list_noneinfo">' + rk.i18n('APPROVAL_SUBMIT_FORM_ERROR_11') + '</span>');
                    var $span = $link.parent().find('span.nc_error_business_required');
                    if ($span) {
                        $span.remove();
                    }
                    $link.remove();
                }
            });
        },
        _handleApproverSelectClick: function(selectAObj) {
            var me = this;
            var elem = me.element;
            var opt = me.options;
            var formContainer = $(selectAObj).parents();
            var userSelect = formContainer.find("#aplUserSelect");
            var approvalUser_hidden = formContainer.find(":hidden.approvalUser_hidden");
            var select_depart_list = formContainer.find("div.select_depart_list");
            var nc_selectColleague_li = select_depart_list.find(".nc_selectColleague_li");
            userSelect.rkUser({
                onSelect: function(userId, userName) {
                    $(selectAObj).text(userName);
                    approvalUser_hidden.val(userId);
                    select_depart_list.hide();
                }
            });
            nc_selectColleague_li.bind("click", function() {
                var approval = $(this).attr("uname");
                approvalUser_hidden.val($(this).attr("uid"));
                $(selectAObj).text(approval);
                select_depart_list.hide();
            });
            if (!select_depart_list.is(":hidden")) {
                select_depart_list.hide();
            } else if (nc_selectColleague_li.size() > 0 || userSelect.size() > 0) {
                select_depart_list.show();
            }
        },
        _accountAutoComplete: function(_suffix) {
            var me = this;
            var elem = me.element;
            var opt = me.options;
            if (typeof _suffix == "undefined") {
                _suffix = "";
            }
            $("#account" + _suffix).autocomplete({
                position: {
                    collision: "flipfit"
                },
                source: function(request, response) {
                    var url = "/json/oa_search/search-account.action?accountTypes=1";
                    var key = request.term;
                    $.ajax({
                        url: url,
                        type: 'POST',
                        minLength: -1,
                        data: {
                            pageNo: 1,
                            key: key.toLowerCase(),
                            accountTypes: 1
                        },
                        dataType: "json",
                        success: function(data, status) {
                            var id;
                            if (data.length > 0) {
                                for (var i = 0; i < data.length; i++) {
                                    if (data[i].label == key) {
                                        id = data[i].value;
                                    }
                                }
                                me._account(id, _suffix)
                            } else {
                                me._account(undefined, _suffix)
                            }
                            response(data);
                        },
                        error: function() {
                            response([]);
                        }
                    });
                },
                select: function(event, ui) {
                    var id = ui.item.value;
                    $("#object_id" + _suffix).val(id);
                    $("#account" + _suffix).val(ui.item.label);
                    if ($("#account_search_msg" + _suffix).length > 0) {
                        $("#account_search_msg" + _suffix).html(rk.i18n('APPROVAL_AUTO_RELATION_6'));
                    }
                    return false;
                }
            });
        },
        _account: function(id, _suffix) {
            var me = this;
            var elem = me.element;
            var opt = me.options;
            if (typeof _suffix == "undefined") {
                _suffix = "";
            }
            if (id != undefined) {
                $("#account_search_msg" + _suffix).html(rk.i18n('APPROVAL_AUTO_RELATION_6')).show();
                $("#object_id" + _suffix).val(id);
            } else {
                $("#account_search_msg" + _suffix).html(rk.i18n('APPROVAL_RESELECT_6')).show();
                $("#object_id" + _suffix).val("");
            }
        },
        _partnerAutoComplete: function(_suffix) {
            var me = this;
            var elem = me.element;
            var opt = me.options;
            if (typeof _suffix == "undefined") {
                _suffix = "";
            }
            $("#partner" + _suffix).autocomplete({
                position: {
                    collision: "flipfit"
                },
                source: function(request, response) {
                    var url = "/json/oa_search/search-account.action?accountTypes=2";
                    var key = request.term;
                    $.ajax({
                        url: url,
                        type: 'POST',
                        minLength: -1,
                        data: {
                            pageNo: 1,
                            key: key.toLowerCase(),
                            accountTypes: 2
                        },
                        dataType: "json",
                        success: function(data, status) {
                            var id;
                            if (data.length > 0) {
                                for (var i = 0; i < data.length; i++) {
                                    if (data[i].label == key) {
                                        id = data[i].value;
                                    }
                                }
                                me._partner(id, _suffix)
                            } else {
                                me._partner(undefined, _suffix)
                            }
                            response(data);
                        },
                        error: function() {
                            response([]);
                        }
                    });
                },
                select: function(event, ui) {
                    var id = ui.item.value;
                    $("#object_id" + _suffix).val(id);
                    $("#partner" + _suffix).val(ui.item.label);
                    if ($("#partner_search_msg" + _suffix).length > 0) {
                        $("#partner_search_msg" + _suffix).html(rk.i18n('APPROVAL_AUTO_RELATION_10'));
                    }
                    return false;
                }
            });
        },
        _partner: function(id, _suffix) {
            var me = this;
            var elem = me.element;
            var opt = me.options;
            if (typeof _suffix == "undefined") {
                _suffix = "";
            }
            if (id != undefined) {
                $("#partner_search_msg" + _suffix).html(rk.i18n('APPROVAL_AUTO_RELATION_10')).show();
                $("#object_id" + _suffix).val(id);
            } else {
                $("#partner_search_msg" + _suffix).html(rk.i18n('APPROVAL_RESELECT_10')).show();
                $("#object_id" + _suffix).val("");
            }
        },
        _oppAutoComplete: function(_suffix) {
            var me = this;
            var elem = me.element;
            var opt = me.options;
            if (typeof _suffix == "undefined") {
                _suffix = "";
            }
            $("#opp" + _suffix).autocomplete({
                position: {
                    collision: "flipfit"
                },
                source: function(request, response) {
                    var key = request.term;
                    $.ajax({
                        url: "/json/oa_search/search-opp.action",
                        type: 'GET',
                        data: {
                            pageNo: 1,
                            key: key.toLowerCase()
                        },
                        dataType: "json",
                        success: function(data, status) {
                            var id;
                            if (data.length > 0) {
                                for (var i = 0; i < data.length; i++) {
                                    if (data[i].label == key) {
                                        id = data[i].value;
                                    }
                                }
                                me._opp(id, _suffix)
                            } else {
                                me._opp(undefined, _suffix)
                            }
                            response(data);
                        },
                        error: function() {
                            response([]);
                        }
                    });
                },
                select: function(event, ui) {
                    var id = ui.item.value;
                    $("#object_id" + _suffix).val(id);
                    $("#opp" + _suffix).val(ui.item.label);
                    if ($("#opp_search_msg" + _suffix).length > 0) {
                        $("#opp_search_msg" + _suffix).html(rk.i18n('APPROVAL_AUTO_RELATION_5'));
                    }
                    return false;
                }
            });
        },
        _opp: function(id, _suffix) {
            var me = this;
            var elem = me.element;
            var opt = me.options;
            if (typeof _suffix == "undefined") {
                _suffix = "";
            }
            if (id != undefined) {
                $("#opp_search_msg" + _suffix).html(rk.i18n('APPROVAL_AUTO_RELATION_5')).show();
                $("#object_id" + _suffix).val(id);
            } else {
                $("#opp_search_msg" + _suffix).html(rk.i18n('APPROVAL_RESELECT_5')).show();
                $("#object_id" + _suffix).val("");
            }
        },
        _contactAutoComplete: function(_suffix) {
            var me = this;
            var elem = me.element;
            var opt = me.options;
            if (typeof _suffix == "undefined") {
                _suffix = "";
            }
            $("#contact" + _suffix).autocomplete({
                position: {
                    collision: "flipfit"
                },
                source: function(request, response) {
                    var url = "/json/oa_search/search-contact.action";
                    var key = request.term;
                    $.ajax({
                        url: url,
                        type: 'POST',
                        data: {
                            pageNo: 1,
                            key: key.toLowerCase()
                        },
                        dataType: "json",
                        success: function(data, status) {
                            var id;
                            if (data.length > 0) {
                                for (var i = 0; i < data.length; i++) {
                                    if (data[i].label == key) {
                                        id = data[i].value;
                                    }
                                }
                                me._contact(id, _suffix)
                            } else {
                                me._contact(undefined, _suffix)
                            }
                            response(data);
                        },
                        error: function() {
                            response([]);
                        }
                    });
                },
                minLength: 1,
                select: function(event, ui) {
                    var id = ui.item.value;
                    $("#object_id" + _suffix).val(id);
                    $("#contact" + _suffix).val(ui.item.label);
                    if ($("#contact_search_msg" + _suffix).length > 0) {
                        $("#contact_search_msg").html(rk.i18n('APPROVAL_AUTO_RELATION_4')).show();
                    }
                    return false;
                }
            });
        },
        _contact: function(id, _suffix) {
            var me = this;
            var elem = me.element;
            var opt = me.options;
            if (typeof _suffix == "undefined") {
                _suffix = "";
            }
            if (id != undefined) {
                $("#contact_search_msg" + _suffix).html(rk.i18n('APPROVAL_AUTO_RELATION_4')).show();
                $("#object_id" + _suffix).val(id);
            } else {
                $("#contact_search_msg" + _suffix).html(rk.i18n('APPROVAL_RESELECT_4')).show();
                $("#contactId").val("");
            }
        },
        _leadAutoComplete: function(_suffix) {
            var me = this;
            var elem = me.element;
            var opt = me.options;
            if (typeof _suffix == "undefined") {
                _suffix = "";
            }
            $("#lead" + _suffix).autocomplete({
                position: {
                    collision: "flipfit"
                },
                source: function(request, response) {
                    var key = request.term;
                    $.ajax({
                        url: "/json/oa_search/search-lead.action",
                        type: 'GET',
                        data: {
                            pageNo: 1,
                            key: key.toLowerCase()
                        },
                        dataType: "json",
                        success: function(data, status) {
                            var id;
                            if (data.length > 0) {
                                for (var i = 0; i < data.length; i++) {
                                    if (data[i].label == key) {
                                        id = data[i].value;
                                    }
                                }
                                me._lead(id, _suffix)
                            } else {
                                me._lead(undefined, _suffix)
                            }
                            response(data);
                        },
                        error: function() {
                            response([]);
                        }
                    });
                },
                select: function(event, ui) {
                    var id = ui.item.value;
                    $("#object_id" + _suffix).val(id);
                    $("#lead" + _suffix).val(ui.item.label);
                    var leadSearchMsg = $("#lead_search_msg" + _suffix);
                    if (leadSearchMsg.length > 0) {
                        leadSearchMsg.html(rk.i18n('APPROVAL_AUTO_RELATION_3'));
                    }
                    return false;
                }
            });
        },
        _lead: function(id, _suffix) {
            var me = this;
            var elem = me.element;
            var opt = me.options;
            if (typeof _suffix == "undefined") {
                _suffix = "";
            }
            if (id != undefined) {
                $("#lead_search_msg" + _suffix).html(rk.i18n('APPROVAL_AUTO_RELATION_3')).show();
                $("#object_id" + _suffix).val(id);
            } else {
                $("#lead_search_msg" + _suffix).html(rk.i18n('APPROVAL_RESELECT_3')).show();
                $("#object_id" + _suffix).val("");
            }
        },
        _campaignAutoComplete: function(_suffix) {
            var me = this;
            var elem = me.element;
            var opt = me.options;
            if (typeof _suffix == "undefined") {
                _suffix = "";
            }
            $("#campaign" + _suffix).autocomplete({
                position: {
                    collision: "flipfit"
                },
                source: function(request, response) {
                    var key = request.term;
                    $.ajax({
                        url: "/json/oa_search/search-campaign.action",
                        type: 'GET',
                        data: {
                            pageNo: 1,
                            key: key.toLowerCase()
                        },
                        dataType: "json",
                        success: function(data, status) {
                            var id;
                            if (data.length > 0) {
                                for (var i = 0; i < data.length; i++) {
                                    if (data[i].label == key) {
                                        id = data[i].value;
                                    }
                                }
                                me._campaign(id, _suffix)
                            } else {
                                me._campaign(undefined, _suffix)
                            }
                            response(data);
                        },
                        error: function() {
                            response([]);
                        }
                    });
                },
                select: function(event, ui) {
                    var id = ui.item.value;
                    $("#object_id" + _suffix).val(id);
                    $("#campaign" + _suffix).val(ui.item.label);
                    var msg = $("#campaign_search_msg" + _suffix);
                    if (msg.length > 0) {
                        msg.html(rk.i18n('APPROVAL_AUTO_RELATION_2'));
                    }
                    return false;
                }
            });
        },
        _campaign: function(id, _suffix) {
            var me = this;
            var elem = me.element;
            var opt = me.options;
            if (typeof _suffix == "undefined") {
                _suffix = "";
            }
            if (id != undefined) {
                $("#campaign_search_msg" + _suffix).html(rk.i18n('APPROVAL_AUTO_RELATION_2')).show();
                $("#object_id" + _suffix).val(id);
            } else {
                $("#campaign_search_msg" + _suffix).html(rk.i18n('APPROVAL_RESELECT_2')).show();
                $("#object_id" + _suffix).val("");
            }
        },
        _expenseAccountAutoComplete: function(_suffix) {
            var me = this;
            var elem = me.element;
            var opt = me.options;
            if (typeof _suffix == "undefined") {
                _suffix = "";
            }
            $("#expenseaccount" + _suffix).autocomplete({
                position: {
                    collision: "flipfit"
                },
                source: function(request, response) {
                    var key = request.term;
                    $.ajax({
                        url: "/json/oa_search/search-expenseaccount.action",
                        type: 'GET',
                        data: {
                            pageNo: 1,
                            key: key.toLowerCase()
                        },
                        dataType: "json",
                        success: function(data, status) {
                            var id;
                            if (data.length > 0) {
                                for (var i = 0; i < data.length; i++) {
                                    if (data[i].label == key) {
                                        id = data[i].value;
                                    }
                                }
                                me._expenseAccount(id, _suffix)
                            } else {
                                me._expenseAccount(undefined, _suffix)
                            }
                            response(data);
                        },
                        error: function() {
                            response([]);
                        }
                    });
                },
                select: function(event, ui) {
                    var id = ui.item.value;
                    $("#object_id" + _suffix).val(id);
                    $("#expenseaccount" + _suffix).val(ui.item.label);
                    var msg = $("#expenseaccount_search_msg" + _suffix);
                    if (msg.length > 0) {
                        msg.html(rk.i18n('APPROVAL_AUTO_RELATION_1'));
                    }
                    return false;
                }
            });
        },
        _expenseAccount: function(id, _suffix) {
            var me = this;
            var elem = me.element;
            var opt = me.options;
            if (typeof _suffix == "undefined") {
                _suffix = "";
            }
            if (id != undefined) {
                $("#expenseaccount_search_msg" + _suffix).html(rk.i18n('APPROVAL_AUTO_RELATION_1')).show();
                $("#object_id" + _suffix).val(id);
            } else {
                $("#expenseaccount_search_msg" + _suffix).html(rk.i18n('APPROVAL_RESELECT_1')).show();
                $("#object_id" + _suffix).val("");
            }
        },
        _agentAutoComplete: function(_suffix) {
            var me = this;
            var elem = me.element;
            var opt = me.options;
            if (typeof _suffix == "undefined") {
                _suffix = "";
            }
            $("#agent" + _suffix).autocomplete({
                position: {
                    collision: "flipfit"
                },
                source: function(request, response) {
                    var key = request.term;
                    $.ajax({
                        url: "/json/oa_search/search-account.action?accountTypes=4",
                        type: 'GET',
                        data: {
                            pageNo: 1,
                            key: key.toLowerCase()
                        },
                        dataType: "json",
                        success: function(data, status) {
                            var id;
                            if (data.length > 0) {
                                for (var i = 0; i < data.length; i++) {
                                    if (data[i].label == key) {
                                        id = data[i].value;
                                    }
                                }
                                me._agent(id, _suffix)
                            } else {
                                me._agent(undefined, _suffix)
                            }
                            response(data);
                        },
                        error: function() {
                            response([]);
                        }
                    });
                },
                select: function(event, ui) {
                    var id = ui.item.value;
                    $("#object_id" + _suffix).val(id);
                    $("#agent" + _suffix).val(ui.item.label);
                    var msg = $("#agent_search_msg" + _suffix);
                    if (msg.length > 0) {
                        msg.html(rk.i18n('APPROVAL_AUTO_RELATION_1'));
                    }
                    return false;
                }
            });
        },
        _agent: function(id, _suffix) {
            var me = this;
            var elem = me.element;
            var opt = me.options;
            if (typeof _suffix == "undefined") {
                _suffix = "";
            }
            if (id != undefined) {
                $("#agent_search_msg" + _suffix).html(rk.i18n('APPROVAL_AUTO_RELATION_1')).show();
                $("#object_id" + _suffix).val(id);
            } else {
                $("#agent_search_msg" + _suffix).html(rk.i18n('APPROVAL_RESELECT_1')).show();
                $("#object_id" + _suffix).val("");
            }
        },
        _terminalAutoComplete: function(_suffix) {
            var me = this;
            var elem = me.element;
            var opt = me.options;
            if (typeof _suffix == "undefined") {
                _suffix = "";
            }
            $("#terminal" + _suffix).autocomplete({
                position: {
                    collision: "flipfit"
                },
                source: function(request, response) {
                    var key = request.term;
                    $.ajax({
                        url: "/json/oa_search/search-account.action?accountTypes=5",
                        type: 'GET',
                        data: {
                            pageNo: 1,
                            key: key.toLowerCase()
                        },
                        dataType: "json",
                        success: function(data, status) {
                            var id;
                            if (data.length > 0) {
                                for (var i = 0; i < data.length; i++) {
                                    if (data[i].label == key) {
                                        id = data[i].value;
                                    }
                                }
                                me._terminal(id, _suffix)
                            } else {
                                me._terminal(undefined, _suffix)
                            }
                            response(data);
                        },
                        error: function() {
                            response([]);
                        }
                    });
                },
                select: function(event, ui) {
                    var id = ui.item.value;
                    $("#object_id" + _suffix).val(id);
                    $("#terminal" + _suffix).val(ui.item.label);
                    var msg = $("#terminal_search_msg" + _suffix);
                    if (msg.length > 0) {
                        msg.html(rk.i18n('APPROVAL_AUTO_RELATION_1'));
                    }
                    return false;
                }
            });
        },
        _terminal: function(id, _suffix) {
            var me = this;
            var elem = me.element;
            var opt = me.options;
            if (typeof _suffix == "undefined") {
                _suffix = "";
            }
            if (id != undefined) {
                $("#terminal_search_msg" + _suffix).html(rk.i18n('APPROVAL_AUTO_RELATION_1')).show();
                $("#object_id" + _suffix).val(id);
            } else {
                $("#terminal_search_msg" + _suffix).html(rk.i18n('APPROVAL_RESELECT_1')).show();
                $("#object_id" + _suffix).val("");
            }
        },
        _contractAutoComplete: function(_suffix) {
            var me = this;
            var elem = me.element;
            var opt = me.options;
            if (typeof _suffix == "undefined") {
                _suffix = "";
            }
            $("#contract" + _suffix).autocomplete({
                position: {
                    collision: "flipfit"
                },
                source: function(request, response) {
                    var url = "/json/oa_search/search-contract.action?";
                    var key = request.term;
                    $.ajax({
                        url: url,
                        type: 'POST',
                        minLength: -1,
                        data: {
                            pageNo: 1,
                            key: key.toLowerCase()
                        },
                        dataType: "json",
                        success: function(data, status) {
                            var id;
                            if (data.length > 0) {
                                for (var i = 0; i < data.length; i++) {
                                    if (data[i].label == key) {
                                        id = data[i].value;
                                    }
                                }
                                me._contract(id, _suffix)
                            } else {
                                me._contract(undefined, _suffix)
                            }
                            response(data);
                        },
                        error: function() {
                            response([]);
                        }
                    });
                },
                select: function(event, ui) {
                    var id = ui.item.value;
                    $("#object_id" + _suffix).val(id);
                    $("#contract" + _suffix).val(ui.item.label);
                    if ($("#contract_search_msg" + _suffix).length > 0) {
                        $("#contract_search_msg" + _suffix).html(rk.i18n('APPROVAL_AUTO_RELATION_11'));
                    }
                    return false;
                }
            });
        },
        _contract: function(id, _suffix) {
            var me = this;
            var elem = me.element;
            var opt = me.options;
            if (typeof _suffix == "undefined") {
                _suffix = "";
            }
            if (id != undefined) {
                $("#contract_search_msg" + _suffix).html(rk.i18n('APPROVAL_AUTO_RELATION_11')).show();
                $("#object_id" + _suffix).val(id);
            } else {
                $("#contract_search_msg" + _suffix).html(rk.i18n('APPROVAL_RESELECT_11')).show();
                $("#object_id" + _suffix).val("");
            }
        },
        _typeChange: function(_suffix) {
            var me = this;
            var elem = me.element;
            var opt = me.options;
            if (typeof _suffix == "undefined") {
                _suffix = "";
            }
            $("#bussiness_type" + _suffix).selectmenu({
                appendTo: 'body',
                position: {
                    collision: "flipfit"
                }
            });
            $("#bussiness_type" + _suffix).on('selectmenuchange', function(evt, ui) {
                var val = $(this).val();
                if (val == 1) {
                    $("#belong_id" + _suffix).val(1);
                    $("#account_span" + _suffix).show();
                    $("#opp_span " + _suffix + " ,#contact_span" + _suffix + ",#lead_span" + _suffix + ",#campaign_span" + _suffix + ",#expenseaccount_span" + _suffix + ",#agent_span" + _suffix + ",#terminal_span" + _suffix + ",#partner_span" + _suffix + ",#contract_span" + _suffix).hide();
                    $("#opp" + _suffix + ",#contact" + _suffix + ",#object_id" + _suffix + " ,#campaign" + _suffix + " ,#expenseaccount" + _suffix + ",#lead" + _suffix + ",#agent" + _suffix + ",#terminal" + _suffix + ",#partner" + _suffix + ",#contract" + _suffix).val("");
                    clearOpp(_suffix);
                    clearContact(_suffix);
                    clearLead(_suffix);
                    clearCampaign(_suffix);
                    clearExpenseAccount(_suffix);
                    clearAgent(_suffix);
                    clearTerminal(_suffix);
                    clearPartner(_suffix);
                    clearContract(_suffix);
                } else if (val == 2) {
                    $("#belong_id" + _suffix).val(3);
                    $("#account_span" + _suffix + " ,#contact_span" + _suffix + " ,#lead_span" + _suffix + ",#campaign_span" + _suffix + ",#expenseaccount_span" + _suffix + ",#agent_span" + _suffix + ",#terminal_span" + _suffix + ",#partner_span" + _suffix + ",#contract_span" + _suffix).hide();
                    $("#opp_span" + _suffix).show();
                    $("#account" + _suffix + " ,#contact" + _suffix + " ,#object_id" + _suffix + " ,#campaign" + _suffix + " ,#expenseaccount" + _suffix + " ,#lead" + _suffix + ",#agent" + _suffix + ",#terminal" + _suffix + ",#partner" + _suffix + ",#contract" + _suffix).val("");
                    clearAccount(_suffix);
                    clearContact(_suffix);
                    clearLead(_suffix);
                    clearCampaign(_suffix);
                    clearExpenseAccount(_suffix);
                    clearAgent(_suffix);
                    clearTerminal(_suffix);
                    clearPartner(_suffix);
                    clearContract(_suffix);
                } else if (val == 3) {
                    $("#belong_id" + _suffix).val(2);
                    $("#contact_span" + _suffix).show();
                    $("#account_span" + _suffix + " ,#opp_span" + _suffix + " ,#lead_span" + _suffix + ",#campaign_span" + _suffix + ",#expenseaccount_span" + _suffix + ",#agent_span" + _suffix + ",#terminal_span" + _suffix + ",#partner_span" + _suffix + ",#contract_span" + _suffix).hide();
                    $("#opp" + _suffix + " ,#account" + _suffix + " ,#object_id" + _suffix + " ,#campaign" + _suffix + " ,#expenseaccount" + _suffix + " ,#lead" + _suffix + ",#agent" + _suffix + ",#terminal" + _suffix + ",#partner" + _suffix + ",#contract" + _suffix).val("");
                    clearOpp(_suffix);
                    clearAccount(_suffix);
                    clearLead(_suffix);
                    clearCampaign(_suffix);
                    clearExpenseAccount(_suffix);
                    clearAgent(_suffix);
                    clearTerminal(_suffix);
                    clearPartner(_suffix);
                    clearContract(_suffix);
                } else if (val == 4) {
                    $("#belong_id" + _suffix).val(11);
                    $("#lead_span" + _suffix).show();
                    $("#account_span" + _suffix + " ,#opp_span" + _suffix + " ,#contact_span" + _suffix + ",#campaign_span" + _suffix + ",#expenseaccount_span" + _suffix + ",#agent_span" + _suffix + ",#terminal_span" + _suffix + ",#partner_span" + _suffix + ",#contract_span" + _suffix).hide();
                    $("#opp" + _suffix + " ,#account" + _suffix + " ,#contact" + _suffix + " ,#campaign" + _suffix + " ,#expenseaccount" + _suffix + " ,#object_id" + _suffix + ",#agent" + _suffix + ",#terminal" + _suffix + ",#partner" + _suffix + ",#contract" + _suffix).val("");
                    clearOpp(_suffix);
                    clearAccount(_suffix);
                    clearContact(_suffix);
                    clearCampaign(_suffix);
                    clearExpenseAccount(_suffix);
                    clearAgent(_suffix);
                    clearTerminal(_suffix);
                    clearPartner(_suffix);
                    clearContract(_suffix);
                } else if (val == 5) {
                    $("#belong_id" + _suffix).val(6);
                    $("#campaign_span" + _suffix).show();
                    $("#account_span" + _suffix + " ,#opp_span" + _suffix + " ,#contact_span" + _suffix + ",#lead_span" + _suffix + ",#expenseaccount_span" + _suffix + ",#agent_span" + _suffix + ",#terminal_span" + _suffix + ",#partner_span" + _suffix + ",#contract_span" + _suffix).hide();
                    $("#opp" + _suffix + " ,#account" + _suffix + " ,#contact" + _suffix + " ,#lead" + _suffix + " ,#expenseaccount" + _suffix + " ,#object_id" + _suffix + ",#agent" + _suffix + ",#terminal" + _suffix + ",#partner" + _suffix + ",#contract" + _suffix).val("");
                    clearOpp(_suffix);
                    clearAccount(_suffix);
                    clearContact(_suffix);
                    clearLead(_suffix);
                    clearExpenseAccount(_suffix);
                    clearAgent(_suffix);
                    clearTerminal(_suffix);
                    clearPartner(_suffix);
                    clearContract(_suffix);
                } else if (val == 7) {
                    $("#belong_id" + _suffix).val(64);
                    $("#expenseaccount_span" + _suffix).show();
                    $("#account_span" + _suffix + " ,#opp_span" + _suffix + " ,#contact_span" + _suffix + ",#lead_span" + _suffix + ",#campaign_span" + _suffix + ",#agent_span" + _suffix + ",#terminal_span" + _suffix + ",#partner_span" + _suffix + ",#contract_span" + _suffix).hide();
                    $("#opp" + _suffix + " ,#account" + _suffix + " ,#contact" + _suffix + " ,#lead" + _suffix + " ,#campaign" + _suffix + " ,#object_id" + _suffix + ",#agent" + _suffix + ",#terminal" + _suffix + ",#partner" + _suffix + ",#contract" + _suffix).val("");
                    clearOpp(_suffix);
                    clearAccount(_suffix);
                    clearContact(_suffix);
                    clearLead(_suffix);
                    clearCampaign(_suffix);
                    clearAgent(_suffix);
                    clearTerminal(_suffix);
                    clearPartner(_suffix);
                    clearContract(_suffix);
                } else if (val == 8) {
                    $("#belong_id" + _suffix).val(66);
                    $("#agent_span" + _suffix).show();
                    $("#account_span" + _suffix + " ,#opp_span" + _suffix + " ,#contact_span" + _suffix + ",#lead_span" + _suffix + ",#campaign_span" + _suffix + ",#expenseaccount_span" + _suffix + ",#terminal_span" + _suffix + ",#partner_span" + _suffix + ",#contract_span" + _suffix).hide();
                    $("#opp" + _suffix + " ,#account" + _suffix + " ,#contact" + _suffix + " ,#lead" + _suffix + " ,#campaign" + _suffix + " ,#object_id" + _suffix + " ,#expenseaccount" + _suffix + ",#terminal" + _suffix + ",#partner" + _suffix + ",#contract" + _suffix).val("");
                    clearOpp(_suffix);
                    clearAccount(_suffix);
                    clearContact(_suffix);
                    clearLead(_suffix);
                    clearCampaign(_suffix);
                    clearExpenseAccount(_suffix);
                    clearTerminal(_suffix);
                    clearPartner(_suffix);
                    clearContract(_suffix);
                } else if (val == 9) {
                    $("#belong_id" + _suffix).val(67);
                    $("#terminal_span" + _suffix).show();
                    $("#account_span" + _suffix + " ,#opp_span" + _suffix + " ,#contact_span" + _suffix + ",#lead_span" + _suffix + ",#campaign_span" + _suffix + ",#expenseaccount_span" + _suffix + ",#agent_span" + _suffix + ",#partner_span" + _suffix + ",#contract_span" + _suffix).hide();
                    $("#opp" + _suffix + " ,#account" + _suffix + " ,#contact" + _suffix + " ,#lead" + _suffix + " ,#campaign" + _suffix + " ,#object_id" + _suffix + " ,#expenseaccount" + _suffix + ",#agent" + _suffix + ",#partner" + _suffix + ",#contract" + _suffix).val("");
                    clearOpp(_suffix);
                    clearAccount(_suffix);
                    clearContact(_suffix);
                    clearLead(_suffix);
                    clearCampaign(_suffix);
                    clearExpenseAccount(_suffix);
                    clearAgent(_suffix);
                    clearPartner(_suffix);
                    clearContract(_suffix);
                } else if (val == 10) {
                    $("#belong_id" + _suffix).val(9);
                    $("#partner_span" + _suffix).show();
                    $("#account_span" + _suffix + " ,#opp_span" + _suffix + " ,#contact_span" + _suffix + ",#lead_span" + _suffix + ",#campaign_span" + _suffix + ",#expenseaccount_span" + _suffix + ",#agent_span" + _suffix + ",#terminal_span" + _suffix + ",#contract_span" + _suffix).hide();
                    $("#opp" + _suffix + " ,#account" + _suffix + " ,#contact" + _suffix + " ,#lead" + _suffix + " ,#campaign" + _suffix + " ,#object_id" + _suffix + " ,#expenseaccount" + _suffix + ",#agent" + _suffix + ",#terminal" + _suffix + ",#contract" + _suffix).val("");
                    clearOpp(_suffix);
                    clearAccount(_suffix);
                    clearContact(_suffix);
                    clearLead(_suffix);
                    clearCampaign(_suffix);
                    clearExpenseAccount(_suffix);
                    clearAgent(_suffix);
                    clearTerminal(_suffix);
                    clearContract(_suffix);
                } else if (val == 11) {
                    $("#belong_id" + _suffix).val(15);
                    $("#contract_span" + _suffix).show();
                    $("#account_span" + _suffix + " ,#opp_span" + _suffix + " ,#contact_span" + _suffix + ",#lead_span" + _suffix + ",#campaign_span" + _suffix + ",#expenseaccount_span" + _suffix + ",#agent_span" + _suffix + ",#terminal_span" + _suffix + ",#partner_span" + _suffix).hide();
                    $("#opp" + _suffix + " ,#account" + _suffix + " ,#contact" + _suffix + " ,#lead" + _suffix + " ,#campaign" + _suffix + " ,#object_id" + _suffix + " ,#expenseaccount" + _suffix + ",#agent" + _suffix + ",#terminal" + _suffix + ",#partner" + _suffix).val("");
                    clearOpp(_suffix);
                    clearAccount(_suffix);
                    clearContact(_suffix);
                    clearLead(_suffix);
                    clearCampaign(_suffix);
                    clearExpenseAccount(_suffix);
                    clearAgent(_suffix);
                    clearTerminal(_suffix);
                    clearPartner(_suffix);
                }
            });

            function clearAccount(_suffix) {
                $("#account_search_msg" + _suffix).html("").hide();
                $("#object_id" + _suffix).val("");
            }

            function clearOpp(_suffix) {
                $("#opp_search_msg" + _suffix).html("").hide();
                $("#object_id" + _suffix).val("");
            }

            function clearContact(_suffix) {
                $("#contact_search_msg" + _suffix).html("").hide();
                $("#object_id" + _suffix).val("");
            }

            function clearLead(_suffix) {
                $("#lead_search_msg" + _suffix).html("").hide();
                $("#object_id" + _suffix).val("");
            }

            function clearCampaign(_suffix) {
                $("#campaign_search_msg" + _suffix).html("").hide();
                $("#object_id" + _suffix).val("");
            }

            function clearExpenseAccount(_suffix) {
                $("#expenseaccount_search_msg" + _suffix).html("").hide();
                $("#object_id" + _suffix).val("");
            }

            function clearAgent(_suffix) {
                $("#agent_search_msg" + _suffix).html("").hide();
                $("#object_id" + _suffix).val("");
            }

            function clearTerminal(_suffix) {
                $("#terminal_search_msg" + _suffix).html("").hide();
                $("#object_id" + _suffix).val("");
            }

            function clearPartner(_suffix) {
                $("#partner_search_msg" + _suffix).html("").hide();
                $("#object_id" + _suffix).val("");
            }

            function clearContract(_suffix) {
                $("#contract_search_msg" + _suffix).html("").hide();
                $("#object_id" + _suffix).val("");
            }
        },
        _initPopUpSelector: function(popupSelector, data) {
            var me = this;
            $("[name=taskid]").val(data.taskid);
            if (data.approvers.length > 0) {
                data.hasApprovers = 1;
                data.approversLength = data.approvers.length;
            } else {
                data.hasApprovers = 0;
            }
            $.each(data.approvers, function(i, o) {
                if (o.icon == '') {
                    o.icon = DEFAULTS.USER_DEFAULT_ICON;
                }
            });
            var tpl = require('oa/tmpl/approval/tpl_approval_confirm.tpl');
            if (data.hasApprovers == 0 && data.allowSelectApprover == 0 && data.isParallelGateway == 0) {
                data.message = rk.i18n('APPROVAL_SUBMIT_CONFIRM_ERROR_CONTENT');
            } else {
                data.message = rk.i18n('APPROVAL_SUBMIT_CONFIRM_CONTENT');
            }
            data.okText = rk.i18n('CONFIRMATION');
            data.cancelText = rk.i18n('CANCEL');
            var html = rk.templateText(tpl, data);
            $(popupSelector).html(html);
            var $dialog = $(popupSelector);
            $dialog.find('a[act="cancel"]').on('click', function() {
                $("#dialog").parent().show();
                rk.closePopup();
            });
            $dialog.find('a[class="close"]').on('click', function() {
                $("#dialog").parent().show();
                rk.closePopup();
            });
            $dialog.find('a[act="ok"]').on('click', function() {
                if ($dialog.find(".approvalUser_hidden:first").size() == 0) {
                    $.msg(rk.i18n('APPROVAL_SUBMIT_FORM_ERROR_7'));
                    return false;
                }
                if ($dialog.find(".approvalUser_hidden:first").val() == "") {
                    $.msg(rk.i18n('APPROVAL_SUBMIT_FORM_ERROR_8'));
                    return false;
                }
                $("#approvalUserId").val($dialog.find(".approvalUser_hidden:first").val());

                var copyUserIds = $.map($('input[approval="selectCopyInput"]').multipeopleselector("val"), function(k) {
                    return k.value;
                }).join(',');
                $("#copyUserIds").val(copyUserIds);
                var copyTo = $("#copyUserIds").val();
                if (copyTo != null && copyTo != '') {
                    /*if (copyTo.toString().indexOf($dialog.find(".approvalUser_hidden:first").val()) >= 0) {
                     $.msg(rk.i18n('APPROVAL_SUBMIT_FORM_ERROR_9'));
                     return false;
                     }*/
                    /*if (copyTo.toString().indexOf(me.$currentUserId) >= 0) {
                     $.msg(rk.i18n('APPROVAL_SUBMIT_FORM_ERROR_10'));
                     return false;
                     }*/
                }

                var attachArray = [];
                if (me.element.find('.fileinput-box').is(':data(rk-multifileuploader)')) {
                    me.element.find('.fileinput-box').multifileuploader('val').forEach(function(file) {
                        attachArray.push({
                            attachName: file.name,
                            attachUrl: file.fileUrl,
                            attachLength: file.size
                        });
                    });
                }
                if (attachArray.length > 9) {
                    $.msg(rk.i18n('APPROVAL_SUBMIT_FORM_ERROR_5'));
                    return false;
                }
                if (attachArray.length > 0) {
                    $("#attachArrayId").val(JSON.stringify(attachArray).toString());
                }
                var formObj = $('#applyUpdateForm');
                var options = {
                    iframe: false,
                    dataType: 'json',
                    success: function(resp) {
                        if (resp.status == 0) {
                            $.msg(rk.i18n('OPERATE_SUCCESS'), 1);
                            me._delay(function() {
                                rk.closePopup(); //关闭dialog
                                rk.oa.closeContent();
                                $(" div.oa-tab-header a.active").click();
                                if (seajs.data.isFinal) {
                                    history.go(0);
                                } else {
                                    me.closeMe();
                                }
                            });
                        } else {
                            rk.closePopup(); //关闭dialog
                            $.msg(rk.i18n('OPERATE_LOSE'));
                        }
                    }
                };
                var applyFormData = $('#applyItemListDiv').rkAutoForm('getData');
                if (applyFormData === false) {
                    return;
                }
                if (applyFormData != null) {
                    var hideHtml = '';
                    $.each(applyFormData, function(i, o) {
                        hideHtml += '<input type="hidden" name="paramMap[\'' + o.key + '\']" value="' + o.val + '"/>';
                    });
                    formObj.append(hideHtml);
                }
                formObj.attr("action", "/json/oa_approval/update-apply.action");
                var approvalTitle = $("[_id='title']")
                if (!approvalTitle.val()) {
                    approvalTitle.val(approvalTitle.attr("placeholder"));
                } else if (approvalTitle.val().length > 100) {
                    $.msg("标题字数不能超过100");
                    return;
                }
                formObj.ajaxSubmit(options);
                $dialog.find('a[act="ok"]').beLoading();
            }).focus();
            if (data.defaultUserName) {
                $("a[approval=approverSelectA]").beTip({
                    dir: 'bottom',
                    closeDelay: false
                })
            }
            $(popupSelector).delegate('a[approval=approverSelectA]', 'click', function() {
                me._handleApproverSelectClick($(this));
            });
            $(popupSelector).delegate('a[approval=aplSelectConfirmA]', 'click', function() {
                $('div.js_nc_show_approval_user').hide();
            });
            $('input[approval="selectCopyInput"]').multipeopleselector({
                position: {
                    my: 'left top+2'
                }
            });
        },
        okClicked: function() {
            var me = this;
            var elem = me.element;
            var formObj = $('#applyUpdateForm');
            var applyFormData = $('#applyItemListDiv').rkAutoForm('getData');
            if (applyFormData === false) {
                return;
            }
            //if (formObj.find(".nc_error_upload_required").size() > 0 && $.trim($("#myFileQueue").text()).length < 1) {
            if (formObj.find(".nc_error_upload_required").size() > 0 && me.element.find('.fileinput-box').multifileuploader('val').length < 1) {
                $.msg(rk.i18n('APPROVAL_SUBMIT_FORM_ERROR_6'));
                return false;
            }
            var attachArray = new Array();
            // $("#myFileQueue .file_a_class").each(function() {
            //     var attachObj = {
            //         attachName: $(this).text(),
            //         attachUrl: $(this).attr("attachUrl"),
            //         attachLength: $(this).attr("attachLength")
            //     };
            //     attachArray.push(attachObj);
            // });
            if (me.element.find('.fileinput-box').is(':data(rk-multifileuploader)')) {
                me.element.find('.fileinput-box').multifileuploader('val').forEach(function(file) {
                    attachArray.push({
                        attachName: file.name,
                        attachUrl: file.url,
                        attachLength: file.size
                    });
                });
            }
            if (attachArray.length > 9) {
                $.msg(rk.i18n('APPROVAL_SUBMIT_FORM_ERROR_5'));
                return false;
            }
            if (attachArray.length > 0) {
                $("#attachArrayId").val(JSON.stringify(attachArray).toString());
            }
            if (formObj.find(".nc_error_business_required").size() > 0 && $.trim($("#object_id").val()) == "") {
                $.msg(rk.i18n('APPROVAL_SUBMIT_FORM_ERROR_4'));
                return false;
            }
            var approvalTitle = $("input[_id='title']");
            if (!approvalTitle.val()) {
                approvalTitle.val(approvalTitle.attr("placeholder"));
            } else if (approvalTitle.val().length > 100) {
                $.msg("标题字数不能超过100");
                return;
            }
            var options = {
                iframe: false,
                success: function(json) {
                    json = JSON.parse(json);
                    if (json.status == 0) {
                        if (json.data.allowSelectApprover == -1) {
                            $.msg("根据您填写的信息，您当前的审批流程无法分配审批人，审批流程无法继续执行，请修改信息，再重新提交");
                            return false;
                        }
                        var myName = 'demoPopup';
                        var myId = "popup_content";
                        rk.popup({
                            name: myName,
                            width: 580,
                            height: 400,
                            target: myId,
                            success: function(popupSelector) {
                                $("#dialog").parent().hide();
                                me._initPopUpSelector(popupSelector, json.data)
                            },
                            afterClose: function() {}
                        });
                    };
                }
            };
            if (applyFormData != null) {
                var hideHtml = '';
                $.each(applyFormData, function(i, o) {
                    formObj.find('input[name="paramMap[\'' + o.key + '\']"]').remove();
                    hideHtml += '<input type="hidden" name="paramMap[\'' + o.key + '\']" value="' + o.val + '"/>';
                });
                formObj.append(hideHtml);
            }
            formObj.attr("action", "/json/oa_approval/find-update-approver.action");
            me.getBtn('ok').beLoading();
            formObj.ajaxSubmit(options);
        },
        cancelClicked: function() {
            var me = this;
            var elem = me.element;
            me.closeMe(); //关闭
        }
    });

    return $;
});
